package cn.storm.codehelper.util

import com.intellij.notification.Notification
import com.intellij.notification.Notifications
import com.intellij.openapi.application.ApplicationManager
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.util.concurrent.TimeUnit

fun notify(notification: Notification, elapse: Long = 4) {
    asyncRun {
        Notifications.Bus.notify(notification)
        TimeUnit.SECONDS.sleep(elapse)
        notification.expire()
    }
}
fun asyncRun(run: () -> Unit) {
    ApplicationManager.getApplication().executeOnPooledThread(run)
}

fun <T> T?.isNull():Boolean{
    return this == null
}

fun <T> T?.isNotNull():Boolean{
    return !this.isNull()
}


fun getSystemClipboardText():String{
    val sysClip = Toolkit.getDefaultToolkit().systemClipboard
    val clipTf = sysClip.getContents(null)
    return  try {
        clipTf?.takeIf { clipTf.isDataFlavorSupported(DataFlavor.stringFlavor) }
            ?.getTransferData(DataFlavor.stringFlavor)
            ?.run { this as String }
            ?: ""
    } catch (e: Exception) {
        ""
    }

}